package cn.rwklyd.ecommerce.controller;

import cn.rwklyd.ecommerce.service.ApiService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author 键盘书生
 * @date 2024/10/20
 */
@RestController
@Slf4j
@RequestMapping("/QRcode")
public class QRcodeController {
    @Autowired
    private ApiService apiService;

    //用户充值余额
//    @GetMapping("/rechargeQRCode")
//    public String rechargeQRCode(@RequestParam String id, @RequestParam String amount, Model model) {
//        log.info("传参数据：{}{}", amount, id);
//       // 增加用户余额
//        Boolean flag =apiService.rechargeQRCode(id, amount);
//
//        // 将结果传递给视图
//        if (flag) {
//            model.addAttribute("message", "支付成功");
//            return "successPage";
//        } else {
//            model.addAttribute("message", "支付出错");
//            return "erroPage";
//        }
//    }

    @GetMapping("/rechargeQRCode")
    public void processPayment(@RequestParam String id, @RequestParam double amount, HttpServletResponse response) throws IOException {
        log.info("传参数据：{}{}", amount, id);
        try {
            // 增加用户余额
            boolean isPaymentSuccessful = apiService.rechargeQRCode(id, amount);
            if (isPaymentSuccessful) {
                // 重定向到支付成功页面
                response.sendRedirect("http://attomall.rwklyd.icu/success");
            } else {
                // 重定向到支付失败页面
                response.sendRedirect("http://attomall.rwklyd.icu/error");
            }
        } catch (Exception e) {
            log.error("处理支付时发生错误", e);
            response.sendRedirect("http://attomall.rwklyd.icu/error");
        }
    }


}
